﻿/*  
    file: FacebookAction.js
    requires: Utilities.js
*/
etui.ui = etui.ui || {};
!function ($, ns) {
    function FacebookAction(actionName) {
        if (!actionName) {
            throw "action parameter require";
        }
        this.actionName = actionName;
        // debug mode
        var interfaces = ["sendRequest", "init", "callback", "setReceiverName"];
        for (var len = interfaces.length; len--; ) {
            if (!this[interfaces[len]]) {
                throw interfaces[len] + " has not been implemented";
            }
        }
    }
    ns.FacebookAction = FacebookAction;
} (jQuery, etui.ui);

!function ($, ns) {
    var defaults = {
        senderPage: location.protocol + "\/\/" + location.host + "/ExternalUser/autologin"
    };
    var genReqUrl = new etui.ui.GenerateRequestUrl();
    function FetchInfoAction() {
        ns.FacebookAction.call(this, "fetchInfo");
    }
    FetchInfoAction.prototype.setReceiverName = function (receiverName) {
        this.receiverName = receiverName;
        this.$context;
    };
    FetchInfoAction.prototype.init = function (selector, options) {
        var settings, $context;
        $context = this.$context = $(selector);

        settings = $.extend(true, defaults, options);
        if (!$context.length) {
            //debug
            //throw { errorMsg: "no such element found!" };
            return this;
        }

        // apply to the first element only.
        $context = $($context[0]);
        this.senderPage = settings.senderPage;
        delete settings.senderPage;
        var redirectUrl = genReqUrl.getUrl(0, this, settings);
        this.url = redirectUrl;
    };
    FetchInfoAction.prototype.callback = function (status) {
        this.externalData = status;
    };
    FetchInfoAction.prototype.sendRequest = function () {
        var iframeStr = etui.n("<iframe src=\"{0}\" style=\"display: none;\"></iframe>")
                            .stringFormat(this.url);
        var $iframe = $(iframeStr);

        this.$context.append($iframe);
        this.winObj = $iframe;
    };
    ns.FetchInfoAction = FetchInfoAction;
} (jQuery, etui.ui);

!function ($, ns) {
    var defaults = {
        senderPage: location.protocol + "\/\/" + location.host + "/ExternalUser/autologin",
        openWinName: "etwin",
        winAttr: "location=0,status=0,toolbar=0,width=500,height=380,resizable=1",
        buttonText: "Login with Facebook",
        loginBtn: "<a class=\"btn_f_connect\" href=\"{0}\"><span class=\"btn_f_connect_l\"></span><span class=\"btn_f_connect_m\">{1}</span><span class=\"btn_f_connect_r\"></span></a>"
    };
    var facebookApiParams = ["client_id",
                             "redirect_uri",
                             "scope",
                             "state",
                             "response_type",
                             "display"];

    var genReqUrl = new etui.ui.GenerateRequestUrl();
    function showBindingPage() {
        var iframe = $("<iframe src=\"" + location.protocol + "\/\/" + location.host + "/online/facebook/bindingaccount\" frameborder=\"0\" width=\"790\" height=\"400\" scrolling=\"no\" overflow=\"hidden\"></iframe>");
        var page = showPopUp("bindingPage", iframe);
    }
    function refreshPage() {
        var page = showPopUp("refreshPage", $("<div><p> you page is being refreshed !</p></div>"));
        //console.log(page);
        setTimeout(function () {
            page.dispose();
            window.location.href = window.location.href;
        }, 2000);
    }
    function showPopUp(name, dom) {
        if (!etPage.popupWin) {
            etPage.popupWin = [];
        }
        showPopUp = function (name, dom) {
            if (etPage.popupWin[name]) {
                etPage.popupWin[name].show();
            }
            else {
                etPage.popupWin[name] = new et.ui.lightbox({ target: dom, customName: "et_js_lightbox_largest" });
                etPage.popupWin[name].show();
            }
            return etPage.popupWin[name];
        }
        return showPopUp(name, dom);
    }
    function facebookLoginHandler(e) {
        e.preventDefault();
        e.data.obj.sendRequest();
    }
    function LoginAction() {
        ns.FacebookAction.call(this, "login");
        this.$context;
        this.url;
        this.winAttr;
        this.winName;
        this.winObj;
    }
    LoginAction.prototype.sendRequest = function () {
        this.winObj = window.open(this.url, this.winName, this.winAttr);
    };
    LoginAction.prototype.callback = function (status) {
        if (status.actionName === "login") {

            this.winObj.close();
            //console.log(status);
            //            if (status.etloggedin) {
            //                refreshPage();
            //            }
            //            else if (status.exloggedin && !status.bound) {
            //                showBindingPage();
            //            }
        }
    };
    LoginAction.prototype.init = function (selector, options) {
        var settings, $context;
        $context = this.$context = $(selector);

        // if no element found.
        if (!$context.length) {
            //debug
            //throw { errorMsg: "no such element found!" };
            return this;
        }
        // apply to the first element only.
        $context = $($context[0]);

        settings = $.extend(true, defaults, options);
        this.senderPage = settings.senderPage;
        var url = genReqUrl.getUrl(1, this);

        var $loginBtn = $(etui.n(settings.loginBtn)
                              .stringFormat(url, settings.buttonText));
        $loginBtn.bind("click", { obj: this }, facebookLoginHandler);
        this.url = url;
        this.winAttr = settings.winAttr;
        this.winName = settings.openWinName;
        $context.append($loginBtn);
        return this;
    };
    LoginAction.prototype.setReceiverName = function (receiverName) {
        this.receiverName = receiverName;
    };

    function AutoLoginAction() {
        ns.FacebookAction.call(this, "autoLogin");
        this.receiverName;
        this.$context;
        this.winObj;
        this.url;
    }
    AutoLoginAction.prototype.setReceiverName = function (receiverName) {
        this.receiverName = receiverName;
    };
    AutoLoginAction.prototype.init = function (selector, options) {
        var settings, $context;
        $context = this.$context = $(selector);

        settings = $.extend(true, defaults, options);
        if (!$context.length) {
            //debug
            //throw { errorMsg: "no such element found!" };
            return this;
        }

        // apply to the first element only.
        $context = $($context[0]);
        this.senderPage = settings.senderPage;
        var redirectUrl = genReqUrl.getUrl(0, this);
        this.url = redirectUrl;
    };
    AutoLoginAction.prototype.callback = function (status) {
        this.winObj.remove();
        if (status.etloggedin) {
            refreshPage();
        }
    };
    AutoLoginAction.prototype.sendRequest = function () {
        var iframeStr = etui.n("<iframe src=\"{0}\" style=\"display: none;\"></iframe>")
                            .stringFormat(this.url);
        var $iframe = $(iframeStr);

        this.$context.append($iframe);
        this.winObj = $iframe;
    };

    ns.LoginAction = LoginAction;
    ns.AutoLoginAction = AutoLoginAction;
} (jQuery, etui.ui);
